<?php

class messageAction extends sfAction
{
    public function execute($request)
    {
        $this->forward404Unless($request->isMethod(sfRequest::POST));
        $email = $request->getParameter('email');
        $issue = $request->getParameter('issue');
        $subject = $request->getParameter('subject');
        $message = $request->getParameter('message');
        $page_id = $request->getParameter('page_id');
        if ($message === '')
            return $this->renderText(json_encode(array( 'status' => 'missing_message')));
        if (!$email) {
            $email_from = array(sfConfig::get('sf_contact_mail_from') => sfConfig::get('sf_contact_name_from'));
            $email = "no email";
        }
        else
            $email_from = $email;

        $mailer = sfContext::getInstance()->getMailer();
        $mailer->composeAndSend(
            $email_from,
            array(sfConfig::get('sf_contact_mail_to') => sfConfig::get('sf_contact_name_to')),
            '['.$issue.'] - '.$subject,
            "New contact message :\n".
            "email: ".$email."\n".
            "page_id: ".$page_id."\n".
            "message:\n".
            $message);

        return $this->renderText('success');
    }
}
